﻿/*
 *
 * Author: 		Zero
 * Version: 	1.2
 * Comments: 	le agregué un type al Contact, que servirá para identificar a los contactos
 * Comments: 	getContacts() ahora devuelve un Contacts que hereda de Collection
 * 
 */
import com.dinmax.flash.central.extension.ExtensionFactory;
import com.dinmax.flash.central.extension.user.*;
import com.dinmax.flash.utilities.xml.XMLUtils;

class ContactFactory {
	
	public static function getContacts(node:XMLNode):Contacts {
		var cnt:Contacts = new Contacts();
		for(var cntcs:XMLNode = node.firstChild; cntcs != null; cntcs=cntcs.nextSibling){
			if(cntcs.nodeType == 1){
				var contact:Contact = ContactFactory.getContact(cntcs);
				cnt.addItem(contact.getPhone(), contact);
			};
		};
		return cnt;
	}; //fin funci	
	
	public static function getContact(node:XMLNode):Contact {
		//trace("ContactFactory.as getContact() node= "+node);
		var cn:Contact = new Contact();
		try {
			cn.setId(Number(XMLUtils.getAttribute(node, "id")));
			cn.setName(XMLUtils.getAttribute(node, "name"));
			cn.setLastName(XMLUtils.getAttribute(node, "lastname"));
			//trace("ContactFactory.as getContact().getLastName() "+cn.getLastName());
			cn.setNick(XMLUtils.getAttribute(node, "nick"));
			cn.setEmail(XMLUtils.getAttribute(node, "email"));
			cn.setPhone(Number(XMLUtils.getAttribute(node, "phonenumber")));
			cn.setType(XMLUtils.getAttribute(node, "type"));
			cn.setVisibility(XMLUtils.getAttribute(node, "visibility"));
			cn.setAddresses(AddressFactory.getAddresses(XMLUtils.getNode(node, "addresses")));
			cn.setExtensions(ExtensionFactory.getExtensions(XMLUtils.getNode(node, "extensions"), true))
			
		} catch (e) {
			//trace("CONTACT FACTORY | ERROR en getContact() "+e);
		};
		return cn;
	}; //fin funci
		
};